package it.gtd.swimpool.controller.resources;

import it.gtd.swimpool.controller.exception.ItemNotFoundException;
import it.gtd.swimpool.model.business.CommunicationManager;
import it.gtd.swimpool.model.entity.User;

import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import java.io.IOException;
import java.io.InputStream;

@Stateless
@Path("/")
public class ProfileResource extends SecuredResource {
    private static final String EMAIL_PASSCHANGE_SUBJECT = "Password reset";
    private static final String EMAIL_PASSCHANGE_BODY =
            "We received a password change request from your account. Your new password is %s. \n" +
            "If you did not require that change, please, contact the administrators.";

    @PersistenceContext
    private EntityManager entityManager;

    @EJB
    private CommunicationManager communicationManager;

    @GET
    @Path("/self")
    @Produces(MediaType.APPLICATION_JSON)
    public User viewYourProfile(
        @CookieParam(Cookie.SESSION_ID) String sessionId
    ) {
        User user = checkUser(sessionId);

        if (null == user) {
            throw new ItemNotFoundException("User id not valid");
        }

        return user;
    }

    @GET
    @Path("/users/{id}")
    @Produces(MediaType.APPLICATION_JSON)
    public User viewUserProfile(
        @PathParam("id") int id
    ) {
        User user = entityManager.find(User.class, id);

        if (null == user) {
            throw new ItemNotFoundException("User id not valid");
        }

        return user;
    }

    @GET
    @Path("/users/{id}/avatar")
    @Produces("image/jpeg")
    public byte[] viewUserAvatar(
            @PathParam("id") int id
    ) {
        byte[] avatar = entityManager.find(User.class, id).getAvatar();

        if (avatar == null || avatar.length == 0) {
            throw new ItemNotFoundException("No Avatar!");
        }

        return avatar;
    }

    @PUT
    @Path("/self")
    public void editYourProfile(
            @CookieParam(Cookie.SESSION_ID) String sessionId,
            User user
    ) {
        User target = checkUser(sessionId);

        if (user.getDescription() != null)
            target.setDescription(escape(user.getDescription()));

        if (user.getEmail() != null)
            target.setEmail(user.getEmail());

        if (user.getInformation() != null)
            target.setInformation(escape(user.getInformation()));

        if (user.getName() != null)
            target.setName(escape(user.getName()));
    }

    @PUT
    @Path("/self/avatar")
    public void editYourAvatar(
        @CookieParam(Cookie.SESSION_ID) String sessionId,
        InputStream inputImage) throws IOException {
        checkUser(sessionId).loadAvatar(inputImage);
    }

    @GET
    @Path("/self/avatar")
    @Produces("image/jpeg")
    public byte[] viewYourAvatar(
        @CookieParam(Cookie.SESSION_ID) String sessionId
    ) {
        byte[] avatar = checkUser(sessionId).getAvatar();

        if (avatar == null || avatar.length == 0) {
            throw new ItemNotFoundException("No Avatar!");
        }

        return avatar;
    }

    @PUT
    @Path("/self/password")
    public void forcePasswordChange(
            @CookieParam(Cookie.SESSION_ID) String sessionId,
            String password
    ) {
        User target = checkUser(sessionId);

        target.setPassword(password);

        communicationManager.sendEmail(target, EMAIL_PASSCHANGE_SUBJECT, String.format(EMAIL_PASSCHANGE_BODY, password));
    }
}
